package com.example.wordtris;


import java.util.Locale;

import com.example.wordtris.WTGameVars;
import com.example.wordtris.MainActivity;
import com.example.wordtris.R;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity {
	 /** Called when the activity is first created. */
		final WTGameVars engine = new WTGameVars();
		Button play,highscore,setting,exit;
		String linguaDefault,nickName;
		Cursor cursor;
		Button bmusic;
    	boolean flag = false;
    	MediaPlayer mp = null;
    	//boolean flag1 = false;
		private Locale locale = null;
		private WordtrisDatabaseHelper WordtrisDatabaseHelper;
		SQLiteDatabase db;
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        WordtrisDatabaseHelper = new WordtrisDatabaseHelper(this);
	        linguaDefault=Locale.getDefault().getLanguage();
	        db = WordtrisDatabaseHelper.getReadableDatabase();
	        cursor =db.query("lingua", null, null, null, null, null, null);
	        if (cursor.getCount()<1) {
	        	db = WordtrisDatabaseHelper.getWritableDatabase();
	        	ContentValues values = new ContentValues();
	        	values.put("lingua", linguaDefault);
	        	db.insert("lingua",null, values);
	        	} else {
	        		cursor.moveToFirst();
	        		linguaDefault=cursor.getString(1);
	        	}
	        cursor.close();
	        db.close();
	        Configuration config = getBaseContext().getResources().getConfiguration();
	        locale = new Locale(linguaDefault);
	        Locale.setDefault(locale);
	        config.locale = locale;
	        getBaseContext().getResources().updateConfiguration(config, 
	        	    getBaseContext().getResources().getDisplayMetrics());
	        setContentView(R.layout.menu_activity);
	        WTGameVars.context = getApplicationContext();
	        //final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.jing); // Audiofile in raw folder
	        mp=MediaPlayer.create(this, R.raw.jing);
	       // string path="H:\Documents and Settings\jaman\Documenti\Google Drive\MyCartella\AndyProject\wordTrisTrunk\res\raw\jing.mp3";
	       // mp.setDataSource(path);
	        //mp.prepare();
	        mp.setLooping(true);
	 		//mp.start();
	        db = WordtrisDatabaseHelper.getReadableDatabase();
	         cursor =db.query("nick", null, null, null, null, null, null);
	         cursor.moveToFirst();
	 		 nickName=cursor.getString(1);
	 		 cursor.close();
	         db.close();
	 		 TextView tv=(TextView) findViewById(R.id.textView1);
	 		 String testo=tv.getText().toString();
	 		 testo+=nickName;
	 		 tv.setText(testo);
	         play =(Button) findViewById(R.id.button1);
	         play.setOnClickListener(new View.OnClickListener()//play on
	         		{
	        	 @Override
	                public void onClick(View aView)
	                 {
	        		 final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);  
	        	   //     mp1.start();
	        		 Intent start = new Intent(getApplicationContext(),startActivity.class);  
	                 startActivity(start); 
	                 
	        		 }
	         		}
	            );  //play off
	         highscore =(Button) findViewById(R.id.button2);
	         highscore.setOnClickListener(new View.OnClickListener()//highscore on
	         		{
	        	 @Override
	                public void onClick(View aView)
	                 {
	        		 final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);  
	     	        mp1.start();
	        		 Intent score = new Intent(getApplicationContext(),highscoreActivity.class);  
	                 startActivity(score); 
	                 }
	         		}	
	        		 );//highscore off
	         setting =(Button) findViewById(R.id.button3);
	         setting.setOnClickListener(new View.OnClickListener()//setting on
	         		{
	        	 @Override
	                public void onClick(View aView)
	                 {
	        		 final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);  
	     	        mp1.start();
	        		 Intent set = new Intent(getApplicationContext(),settingActivity.class);  
	                 startActivity(set); 
	                 }
	         		}	
	        		 );//setting off
	         exit =(Button) findViewById(R.id.buttonExit);
	         exit.setOnClickListener(new View.OnClickListener()//exit on
	          		{
	         	 @Override
	                 public void onClick(View aView)
	                  { 
	         		final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);  
	    	        mp1.start();
	         		/*boolean clean = false;
					clean = engine.onExit(aView);	
					if (clean)
					{
						int pid= android.os.Process.myPid();
						android.os.Process.killProcess(pid);
					}*/
	    	        mp.stop();
	         		finish();
	                Intent intent = new Intent(Intent.ACTION_MAIN);
	                intent.addCategory(Intent.CATEGORY_HOME);
	                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	                startActivity(intent);
	                  }
	          		}	
	         		 );//exit off
	            bmusic =(Button) findViewById(R.id.buttonMusic);
		        bmusic.setOnClickListener(new View.OnClickListener()//bmusic on
		         		{
		        	 @Override
		                public void onClick(View aView)
		                 {
		        		 
		        		 final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);
		        		 mp1.start();
		        		 if(flag){
		                     bmusic.setBackgroundResource(R.drawable.music_on);
		                     flag = false;
		                     mp.start();
		                }else{
		                     bmusic.setBackgroundResource(R.drawable.music_off);
		                     flag = true;
		                     mp.pause();
		               }
		                 }
		         		}	
		        		 );//bmusic off
		        
	    }
	    
	    public void onRestart() {
	        super.onRestart();
	        
	 		db = WordtrisDatabaseHelper.getReadableDatabase();
	        cursor =db.query("lingua", null, null, null, null, null, null);
	        cursor.moveToFirst();
    		linguaDefault=cursor.getString(1);
    		cursor.close();
	        db.close();
	        Configuration config = getBaseContext().getResources().getConfiguration();
	        locale = new Locale(linguaDefault);
	        Locale.setDefault(locale);
	        config.locale = locale;
	        getBaseContext().getResources().updateConfiguration(config, 
	        	    getBaseContext().getResources().getDisplayMetrics());
	        setContentView(R.layout.menu_activity);
	        WTGameVars.context = getApplicationContext();
	        //final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.jing); // Audiofile in raw folder
	        if(mp==null)
	        {
	        	mp.setLooping(true);
		 		mp.start();	
	        }
	        
	        TextView tv=(TextView) findViewById(R.id.textView1);
	 		String testo=tv.getText().toString();
	        testo="";
	        db = WordtrisDatabaseHelper.getReadableDatabase();
	        cursor =db.query("nick", null, null, null, null, null, null);
	        cursor.moveToFirst();
	 		nickName=cursor.getString(1);
	 		cursor.close();
	        db.close();
	 		testo+=nickName;
	 		tv.setText(testo);
	 		play =(Button) findViewById(R.id.button1);
	        play.setOnClickListener(new View.OnClickListener()//play on
	         		{
	        	 @Override
	                public void onClick(View aView)
	                 {
	        		 final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);  
	        	        mp1.start();
	        		 Intent start = new Intent(getApplicationContext(),startActivity.class);  
	                 startActivity(start); 
	                 
	        		 }
	         		}
	            );  //play off
	         highscore =(Button) findViewById(R.id.button2);
	         highscore.setOnClickListener(new View.OnClickListener()//highscore on
	         		{
	        	 @Override
	                public void onClick(View aView)
	                 {
	        		 final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);  
	     	        mp1.start();
	        		 Intent score = new Intent(getApplicationContext(),highscoreActivity.class);  
	                 startActivity(score); 
	                 }
	         		}	
	        		 );//highscore off
	         setting =(Button) findViewById(R.id.button3);
	         setting.setOnClickListener(new View.OnClickListener()//setting on
	         		{
	        	 @Override
	                public void onClick(View aView)
	                 {
	        		 final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);  
	     	        mp1.start();
	        		 Intent set = new Intent(getApplicationContext(),settingActivity.class);  
	                 startActivity(set); 
	                 }
	         		}	
	        		 );//setting off
	         exit =(Button) findViewById(R.id.buttonExit);
	         exit.setOnClickListener(new View.OnClickListener()//exit on
	          		{
	         	 @Override
	                 public void onClick(View aView)
	                  { 
	         		final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);  
	    	    //    mp1.start();
	         		/*boolean clean = false;
					clean = engine.onExit(aView);	
					if (clean)
					{
						int pid= android.os.Process.myPid();
						android.os.Process.killProcess(pid);
					}*/
	    	        mp.stop();
	         		finish();
	                Intent intent = new Intent(Intent.ACTION_MAIN);
	                intent.addCategory(Intent.CATEGORY_HOME);
	                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	                startActivity(intent);
	                  }
	          		}	
	         		 );//exit off
	            bmusic =(Button) findViewById(R.id.buttonMusic);
		        bmusic.setOnClickListener(new View.OnClickListener()//bmusic on
		         		{
		        	 @Override
		                public void onClick(View aView)
		                 {
		        		 
		        		 final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);
		        		 mp1.start();
		        		 if(flag){
		                     bmusic.setBackgroundResource(R.drawable.music_on);
		                     flag = false;
		                     mp.start();
		                }else{
		                     bmusic.setBackgroundResource(R.drawable.music_off);
		                     flag = true;
		                     mp.pause();
		               }
		                 }
		         		}	
		        		 );//bmusic off

	        }
	    
	}

